package com.wang.template

import com.wang.entity.DEFAULT_DATA_CONFIG
import com.wang.entity.DataConfig
import com.wang.utils.DatabaseUtils
import com.wang.utils.StringUtils
import com.wang.utils.TemplateUtils
import java.io.File

fun main() {
    val config = DEFAULT_DATA_CONFIG.copy(database = "wave_apartment_user")
    SQLTemplate.run(
        config, "com.wang", "src/main/resource/target/"
    )
}

object SQLTemplate {
    fun run(config: DataConfig, pkg: String, targetDir: String) {
        run(config.host, config.port, config.database, config.user, config.pwd, pkg, targetDir)
    }

    fun run(
        localhost: String,
        port: String,
        database: String,
        user: String,
        password: String,
        pkg: String,
        targetDir: String ) {

        val tableList = DatabaseUtils.loadDatabase(localhost, port, database, user, password).filter { it.name.equals("landlord_auth_rel") }
        for (table in tableList) {
            for (key in listOf("dao", "entity", "mybatis")) {
                val map = mapOf(
                    "pkg" to pkg,
                    "table" to table,
                    "f" to StringUtils,
                    "name" to StringUtils.upperCamelName(table.name)
                )
                println(TemplateUtils.generate(key, map, File(targetDir, key)).absolutePath)
            }
        }
    }
}